<template>
  <div>
    <el-form :model="addData" ref="addform" label-width="120px" size="small">
      <el-form-item label="横幅名称">
        <el-input v-model="addData.name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="横幅图片">
        <select-img @selectData="data => addData.pic_url = data[0]"/>
      </el-form-item>
      <el-form-item v-if="addData.pic_url !== ''">
        <l-pic :picurl="addData.pic_url" :size="{width:100, height:100}" :disdel="false" :disview="false"/>
      </el-form-item>
      <el-form-item label="跳转链接">
        <el-input v-model="addData.jump_url" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="addData.type">
          <!--<el-radio label="1">微信端</el-radio>-->
          <el-radio label="2">小程序</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="addData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getScoreGoodsList, postBanner, putBanner } from '@/api/score'
import SelectImg from '@/components/SelectImg'
import LPic from '@/components/lPic'

export default {
  props: {
    addData: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  components: {
    SelectImg,
    LPic
  },
  data() {
    return {
      scoreList: [{id: '未选择',name:'未选择'}]
    }
  },
  mounted() {
    this.getScoreGoodsList();
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postBanner(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putBanner(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 获取积分商品列表
     */
    getScoreGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        parent_id: '0'
      };
      getScoreGoodsList(params).then(res => {
        if(res.status === 200){
          this.scoreList = [{id: '未选择',name:'未选择'}, ...res.data];
        }else if(res.status === 204){
          this.scoreList = [{id: '未选择',name:'未选择'}];
        }else{
          this.$message.error(res.message);
        }
      })
    },
  },
}
</script>